/*
  1.创建两个线程，线程2传入线程1的线程id
  2.线程的取消状态默认为 “响应”
  3.线程的取消类型默认为 ”延时相应“
  4.线程1中设置取消类型为 “立即相应”
  5.在线程2中取消线程1
*/

#include "spu.h"


void *thread_fun1(void *arg)
{
    //立即相应取消请求
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
    for(int i = 0; i< 10; i++)
    {
        printf("thread01111 : %d\n", i);
        sleep(1);
    }
	return (void*)0;
}

void *thread_fun2(void *arg)
{
    for(int i = 10; i< 20; i++)
    {
        printf("thread02222 : %d\n", i);
        sleep(2);
        if(i == 12)
        {
            printf("取消线程1\n");
            if( 0 == pthread_cancel((pthread_t)arg))
                printf("取消线程1成功！\n");
        } 
    }
	return (void*)0;
}

int main(int argc, char *argv[])
{	
	int err1, err2;
	pthread_t thread_id1,thread_id2; 

    void *__retval;

	err1 = pthread_create(&thread_id1, NULL, thread_fun1, NULL);
    err2 = pthread_create(&thread_id2, NULL, thread_fun2, (void*)thread_id1);

	if(err1 || err2)
		printf("create failed!\n");
	else
		printf("create success!\n");


    pthread_exit(__retval);
}

